//index.js
//获取应用实例
const app = getApp();
const moment = require('../../moment/moment.min.js')
Page({
  data: {
    // sj: app.sjzh(),//调用公共appjs的方法
    navLists: [
      { "name": "全部",url: "all"},
      { "name": "精华", url: "good"},
      { "name": "分享", url: "share" },
      { "name": "问答", url: "ask" },
      { "name": "招聘", url: "job" },
      { "name": "客户端测试", url: "dev"},
      { "name": "客户端前端开发", url: "all"}
    ], 
    current:0,
    scrollTop:0,
    data:'',
    page: 2,
    sendId:'',
    urls:"https://cnodejs.org/api/v1/topics/?tab=all",
    index: '',
    id: '',
    success:false
  },
  changeNav(e) {
    var _this = this
    let idx = e.currentTarget.id;
    this.setData({
      current: idx
    })
    let urll = 'https://cnodejs.org/api/v1/topics/?tab=' + _this.data.navLists[idx].url
    wx.request({
      url: urll,
      data: '',
      header: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        _this.sjfz(res.data.data)
        _this.setData({
          data: res.data.data,
          urls: urll
        })
      },
      fail: function (res) {
      },
      complete: function (res) {
      },
    })
  },
  onPageScroll(e){
    this.setData({
      scrollTop:e.scrollTop
    })
  },
  toTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
  },
  onLoad(){
    var _this = this;
    wx.request({
      url: 'https://cnodejs.org/api/v1/topics/?tab=all',
      data: '',
      header: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        _this.sjfz(res.data.data)
        _this.setData({
          data: res.data.data,
        })
      },
      fail: function(err) {
        console.log(err)
      }
    })
  },
  onReachBottom: function () {
    var that = this;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    // 页数+1
    wx.request({
      url: that.data.urls+'&page='+that.data.page,
      data: {},
      method: "GET",
      // 请求头部
      header: {
        'content-type': 'application/text'
      },
      success: function (res) {
        that.sjfz(res.data.data)
        that.setData({
          data: res.data.data,
        })
        that.setData({
          data: that.data.data.concat(res.data.data),
          page: that.data.page + 1
        })
        that.toTop()
      } 
    })
    setTimeout(()=>{
      wx.hideLoading()
    },1000)
  },
  sjfz(arr){
    for (let i = 0; i < arr.length; i++) {
      arr[i].last_reply_at = moment(arr[i].last_reply_at).locale('zh-cn').fromNow()
    }
  }
   // onPullDownRefresh: function () {
  //   // 显示顶部刷新图标
  //   setTimeout(()=>{
  //     wx.showNavigationBarLoading();
  //     // 隐藏导航栏加载框
  //     wx.hideNavigationBarLoading();
  //     // 停止下拉动作
  //     wx.stopPullDownRefresh();
  //   },500)
  // }
})
